package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsStorage;
import com.qf.service.DtsStorageService;
import com.qf.util.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@RestController //
@RequestMapping("/admin/storage")
@CrossOrigin
@Slf4j
public class StorageController {

    @Autowired
    private DtsStorageService dtsStorageService;
    /**
     * 分页查询
     */
    @RequiresPermissions("admin:storage:list")
    @RequiresPermissionsDesc(menu = {"系统管理","对象存储"},button = "查询")
    @GetMapping("/list")
    public Object storageList(Integer page, Integer limit, String name, String key, String sort, String order){
        //MybatisPlus: 分页对象
        IPage<DtsStorage> ipage = dtsStorageService.selectPage(page,limit,name,key,sort,order);
        Map<String,Object> result = new HashMap<>();
        result.put("total",ipage.getTotal());
        result.put("items",ipage.getRecords());

        return ResponseUtil.ok(result);

    }
    @RequiresPermissions("admin:storage:create")
    @RequiresPermissionsDesc(menu = {"系统管理","对象存储"},button = "添加")
    @PostMapping("/create")
    public Object create(MultipartFile file) throws Exception{//上传文件

        String originalFilename = file.getOriginalFilename();//上传时候的文件的名称 叫原始名称
        long size = file.getSize();
        byte[] fileBytes = file.getBytes();

        DtsStorage dtsStorage = dtsStorageService.create(originalFilename,size,fileBytes);
        return ResponseUtil.ok(dtsStorage);
    }
    /**
     * 删除
     */
    @RequiresPermissions("admin:storage:delete")
    @RequiresPermissionsDesc(menu = {"系统管理","对象存储"},button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsStorage dtsStorage){

        //删除 或是更新某个deleted
        dtsStorageService.delete(dtsStorage);


        return ResponseUtil.ok();

    }
}
